package org.eclipse.pde.toolbox.adaptors.internal;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.eclipse.core.runtime.IContributor;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.Platform;

public class NamespaceRoot implements IRoot{

	public static final NamespaceRoot instance=new NamespaceRoot();
	private NamespaceParent[] children;
	
	public NamespaceRoot(){		
	}
	
	@Override
	public ITreeNode[] getChildren() {
		// TODO Auto-generated method stub
		IExtensionRegistry extensionRegistry=Platform.getExtensionRegistry();
		IExtensionPoint point= extensionRegistry.getExtensionPoint("org.eclipse.core.runtime.adapters");
		IExtension[] extensions=point.getExtensions();
		
		Map cuplat=new HashMap();
		
		for(int i=0;i<extensions.length;i++){
			
			IContributor contributor=extensions[i].getContributor();
			Set contrExt=(Set) cuplat.get(contributor);
			if (contrExt ==null){
				contrExt=new HashSet();
				cuplat.put(contributor, contrExt);
			}
			contrExt.add(extensions[i]);
			
		}
		
		Set entry=cuplat.entrySet();
		children=new NamespaceParent[entry.size()];
		int k=0;
		for (Iterator it=entry.iterator();it.hasNext();){
			Map.Entry e = (Map.Entry) it.next();
			children[k]=new NamespaceParent((IContributor)e.getKey(),(Set)e.getValue() );
			k++;
		}
		
		return children;
	}

	@Override
	public boolean isTopLevelElement(Object element) {
		// TODO Auto-generated method stub
		if (element instanceof NamespaceParent)
			return true;
		return false;
	}

}
